package icu.wdtt.controller;


import icu.wdtt.Utils.ResultUtils;
import icu.wdtt.enumModel.HttpRequestMessage;
import icu.wdtt.model.Authority;
import icu.wdtt.model.Result;
import icu.wdtt.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("authority")
@ApiModel(value = "权限接口",description = "用于判断各种权限的接口,一般使用不到")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private ResultUtils resultUtils;


    @GetMapping(path = "get",params = "aid")
    public Result get(int aid){
            Authority authority = authorityService.get(aid);
            if(authority != null){
                return resultUtils.success(HttpRequestMessage.SUCCESS_QUERY,authority);
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_QUERY);
    }

    @GetMapping("getAll")
    public Result getAll(){
            List<Authority> authorityList = authorityService.getAll();
            if(authorityList != null){
                return resultUtils.success(HttpRequestMessage.SUCCESS_QUERY,authorityList,authorityList.size());
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_DELETE);

    }
    @PostMapping("add")
    public Result add(Authority authority){

            Boolean flag = authorityService.add(authority);
            if(flag){
                return resultUtils.success(HttpRequestMessage.SUCCESS_ADD);
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_ADD);
    }
    @PostMapping(path = "del",params = "aid")
    public Result del(int aid){

            Boolean flag = authorityService.del(aid);
            if(flag){
                return resultUtils.success(HttpRequestMessage.SUCCESS_DELETE);
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_DELETE);
    }
    @PostMapping("update")
    public Result update(Authority authority){

            Boolean flag = authorityService.update(authority);
            if(flag){
                return resultUtils.success(HttpRequestMessage.SUCCESS_UPDATE);
            }
        return resultUtils.fail(HttpRequestMessage.FAIL_UPDATE);
    }
}
